package com.tianji.remark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
import com.tianji.common.utils.ObjectUtils;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.remark.domain.dto.LikeRecordFormDTO;
import com.tianji.remark.domain.po.LikedRecord;
import com.tianji.remark.mapper.LikedRecordMapper;
import com.tianji.remark.service.ILikedRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.tianji.common.constants.MqConstants.Exchange.LIKE_RECORD_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author ffk
 * @since 2024-06-28
 */
@Slf4j
@RequiredArgsConstructor
//@Service
public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {


    private  final RabbitMqHelper rabbitMqHelper;

    @Override
    public void clickLikes(LikeRecordFormDTO likeRecordFormDTO) {
        //是否是点赞，是否执行成功
        boolean flag= likeRecordFormDTO.getLiked()? likeIt(likeRecordFormDTO) : cancealLikeIt(likeRecordFormDTO);

        //执行新增，删除失败，直接返回
        if(!flag){
            return ;
        }
         Integer countLikes = this.lambdaQuery()
                .eq(LikedRecord::getUserId, UserContext.getUser())
                .eq(LikedRecord::getBizId, likeRecordFormDTO.getBizId())
                .count();

        //发送消息去更新
        //format这里是拼接形成routingkey,前面的常量中有一个占位符，后面根据类型占位，如QA
        rabbitMqHelper.send(LIKE_RECORD_EXCHANGE,
                            StringUtils.format(LIKED_TIMES_KEY_TEMPLATE, likeRecordFormDTO.getBizType())
                            , LikedTimesDTO.of(likeRecordFormDTO.getBizId(),countLikes));
    }


    /**
     * 根据ids查询点赞状态
     * @param bizIds 业务id
     * @return 点过赞的业务id
     */
    @Override
    public Set<Long> queryLikedStatus(List<Long> bizIds) {

        List<LikedRecord> infoList = lambdaQuery()
                .in(LikedRecord::getBizId, bizIds)
                .eq(LikedRecord::getUserId, UserContext.getUser())
                .list();
        return infoList.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
    }

    @Override
    public void readLikedTimesAndSendMessage(String bizType, int maxBizSize) {

    }

    //不喜欢
    private boolean cancealLikeIt(LikeRecordFormDTO likeRecordFormDTO) {
        boolean result = this.remove(new QueryWrapper<LikedRecord>()
                .eq("biz_id", likeRecordFormDTO.getBizId())
                .eq("user_id", UserContext.getUser()));

        return result;
    }

    //喜欢
    private boolean likeIt(LikeRecordFormDTO likeRecordFormDTO) {
        LikedRecord one = lambdaQuery().eq(LikedRecord::getBizId, likeRecordFormDTO.getBizId())
                .eq(LikedRecord::getUserId, UserContext.getUser())
                .one();

        if(ObjectUtils.isNotEmpty(one)){
            //已经点赞了
            return true;
        }

        //否则为空，那么新增喜欢
         LikedRecord addResult = new LikedRecord()
                .setBizType(likeRecordFormDTO.getBizType())
                .setUserId(UserContext.getUser())
                .setBizId(likeRecordFormDTO.getBizId());
        boolean save = save(addResult);

        if(save){
            return true;
        }
        return false;
    }
}
